package com.yao.mall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yao.mall.common.ErrorCode;
import com.yao.mall.common.Result;
import com.yao.mall.config.BusinessException;
import com.yao.mall.model.dto.CategoryRequest;
import com.yao.mall.model.dto.CategorySearchRequest;
import com.yao.mall.model.entity.Category;
import com.yao.mall.model.vo.*;
import com.yao.mall.service.AddressService;
import com.yao.mall.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jiahua.yao
 * @version 1.0
 * @description 分类管理控制类
 * @date 2024/3/5 11:20
 */
@RestController
@RequestMapping("/category")
@Slf4j
@Tag(name = "分类管理相关接口")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @GetMapping("/get")
    //商品添加，三步策略：1.1 获取父分类和对应的子类
    @Operation(summary = "获取父分类和对应的子类")
    public Result<List<CategoryVO>> getCategory() {
        List<CategoryVO> categoryVOS = categoryService.getCategory();
        return Result.success(categoryVOS);
    }
    @PostMapping("/getByConditions")
    @Operation(summary = "条件获取分类")
    public Result<Page<Category>> getByConditions(@RequestBody CategorySearchRequest categorySearchRequest) {
        Page<Category> categoryList = categoryService.getByConditions(categorySearchRequest);
        return Result.success(categoryList);
    }

    @GetMapping("/getCategoryAndGoods")
    @Operation(summary = "获取页面的分类和商品基本信息")
    public Result<List<CategoryVO>> getCategoryAndGoods() {
        List<CategoryVO> categoryVOList = categoryService.getCategoryAndGoodsVO();
        return Result.success(categoryVOList);
    }

    @GetMapping("/getCategoryAndGoodsByParentId")
    //获得子分类getCategoryAndGoodsByParentId
    @Operation(summary = "通过父分类获取分类和对应商品")
    public Result<CategoryVO> getCategoryAndGoodsByParentId(Integer id) {
        if(id == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        CategoryVO categoryVOS = categoryService.getCategoryByParentId(id);
        return Result.success(categoryVOS);
    }

    @GetMapping("/getCategoryAndGoodsBySecId")
    @Operation(summary = "通过子分类获取分类及其对应商品")
    public Result<SecCategoryVO> getCategoryAndGoodsBySectId(Integer id) {
        if(id == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        SecCategoryVO secCategoryVOs = categoryService.getCategoryBySecId(id);
        return Result.success(secCategoryVOs);
    }

    @GetMapping("/getParent")
    @Operation(summary = "获得父分类")
    public Result<List<Category>> getParentName() {
        List<Category> categoryVOS = categoryService.getParentName();
        return Result.success(categoryVOS);
    }

    @GetMapping("/getChild")
    @Operation(summary = "获得子分类")
    public Result<List<Category>> getChildName() {
        List<Category> categoryVOS = categoryService.getChildName();
        return Result.success(categoryVOS);
    }

    @PostMapping("/getBrandAndAttrKey")
    //商品添加，三步策略：1.2 由选择的分类得到对应的品牌以及属性key
    @Operation(summary = "通过分类获取品牌和属性key")
    public Result<BrandAndAttrKeyVO> getBrandAndAttrKeyByCategory(@RequestBody CategoryRequest categoryRequest) {
        BrandAndAttrKeyVO brandAndAttrKeyVO = categoryService.getBrandAndAttrKeyByCategory(categoryRequest);
        return Result.success(brandAndAttrKeyVO);
    }


    @PostMapping("/add")
    @Operation(summary = "添加分类")
    public Result<Boolean> addCategory(@RequestBody Category category) {
        Boolean aBoolean = categoryService.addCategory(category);
        return Result.success(aBoolean);
    }

    @PostMapping("/update")
    @Operation(summary = "更新分类")
    public Result<Boolean> updateCategory(@RequestBody Category category) {
        Boolean aBoolean = categoryService.updateCategory(category);
        return Result.success(aBoolean);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除分类")
    public Result<Boolean> deleteCategory(@PathVariable Long id) {
        Boolean aBoolean = categoryService.deleteCategory(id);
        return Result.success(aBoolean);
    }



}
